package view.hero
{
import framework.view.components.container.IRenderProxy;
import framework.view.components.container.ListProxy;
import framework.view.components.container.PaginationListProxy;

import values.avatar.IAvatarValue;

/**
 * HeroListProxy
 * @author jliang
 *
 */
public class HeroListProxy extends PaginationListProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function HeroListProxy()
	{
		super();
	}
	//==========================================================================
	//  Properties
	//==========================================================================
	public var onHeroSelectedHandler:Function;
	//==========================================================================
	//  Overridden methods: PaginationListProxy
	//==========================================================================
	override protected function get repeaterItemRenderClass():Class
	{
		return HeroItemRenderProxy;
	}
	
	override public function dispose():void
	{
		onHeroSelectedHandler = null;
		super.dispose();
	}
	
	override public function setData(data:*):void
	{
		super.setData(data);
		
		itemRepeater.selectedIndex = 0;
	}
	
	override protected function itemSelectedHandler(render:IRenderProxy):void
	{
		if (onHeroSelectedHandler != null)
		{
			onHeroSelectedHandler(render.data);
		}
	}
	//==========================================================================
	//  Public methods
	//==========================================================================
	public function selectHero(avatar:IAvatarValue):void
	{
		var index:int = -1;
		for (var i:int = 0; i < data.length; i++)
		{
			if (data[i] == avatar)
			{
				index = i;
				break;
			}
		}
		
		var page:int = Math.floor(index / preCount);
		index = index - page * preCount;
		
		updateList(data, data.length, page);
		itemRepeater.selectedIndex = index;		
	}
}
}





